package com.anry.weatherforecast.service;

import com.anry.weatherforecast.utils.FixedAliYunSignatureUtil;
import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 阿里云短信服务实现类（与天气服务风格一致）
 */
@Service
public class AliYunSmsService {
    
    // API配置
    private static final String API_PATH = "/sms/smsSend";
    private static final String API_URL = "https://gyytz.market.alicloudapi.com/sms/smsSend";

    // 应用凭证
    @Value("${anry.appkey}")
    private String APP_KEY ;
    @Value("${anry.appsecret}")
    private  String APP_SECRET;

    private static final String templateId = "908e94ccf08b4476ba6c876d13f084ad";
    private static final String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";

    private final OkHttpClient client;
    private final ObjectMapper objectMapper;
    
    public AliYunSmsService() {
        this.client = createUnsafeOkHttpClient();
        this.objectMapper = new ObjectMapper();
    }

    /**
     * 创建跳过SSL验证的HTTP客户端
     */
    private OkHttpClient createUnsafeOkHttpClient() {
        try {
            javax.net.ssl.X509TrustManager trustManager = new javax.net.ssl.X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[]{};
                }
            };

            javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL");
            sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, new java.security.SecureRandom());
            
            return new OkHttpClient.Builder()
                    .sslSocketFactory(sslContext.getSocketFactory(), trustManager)
                    .hostnameVerifier((hostname, session) -> true)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .build();
                    
        } catch (Exception e) {
            throw new RuntimeException("创建HTTP客户端失败", e);
        }
    }

    /**
     * 发送短信验证码
     *
     * @param mobile 手机号
     * @param code 验证码
     * @param minute 有效分钟数
     * @return 响应结果
     */
    public String sendVerificationCode(String mobile, String code, int minute) throws Exception {
        System.out.println("=== 开始发送短信验证码 ===");

        // 准备查询参数
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("mobile", mobile);
        queryParams.put("templateId", templateId); // 测试模板ID
        queryParams.put("smsSignId", smsSignId); // 测试签名ID
        queryParams.put("param", "**code**:" + code + ",**minute**:" + minute); // 模板变量

        // 发送短信请求
        return sendSmsRequest(queryParams);
    }
    
    /**
     * 发送通用短信
     * 
     * @param mobile 手机号
     * @param templateId 模板ID
     * @param smsSignId 签名ID
     * @param param 模板参数
     * @return 响应结果
     */
    public String sendSms(String mobile, String templateId, String smsSignId, String param) throws Exception {
        System.out.println("=== 开始发送通用短信 ===");
        
        // 准备查询参数
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("mobile", mobile);
        queryParams.put("templateId", templateId);
        queryParams.put("smsSignId", smsSignId);
        
        if (param != null && !param.isEmpty()) {
            queryParams.put("param", param);
        }
        
        // 发送短信请求
        return sendSmsRequest(queryParams);
    }
    
    /**
     * 发送短信请求的核心方法
     * 
     * @param queryParams 查询参数
     * @return 响应结果
     */
    private String sendSmsRequest(Map<String, String> queryParams) throws Exception {
        System.out.println("开始构造短信请求");
        
        // 准备签名参数
        String timestamp = FixedAliYunSignatureUtil.getCurrentTimestamp();
        String nonce = FixedAliYunSignatureUtil.generateNonce();
        
        // 参与签名的Headers
        Map<String, String> signHeaders = new HashMap<>();
        signHeaders.put("X-Ca-Key", APP_KEY);
        signHeaders.put("X-Ca-Timestamp", timestamp);
        signHeaders.put("X-Ca-Nonce", nonce);
        
        String signatureHeaders = FixedAliYunSignatureUtil.getSignatureHeaders(signHeaders);
        System.out.println("参与签名的Headers: " + signatureHeaders);

        // 计算签名时使用带 charset 的 Content-Type
        String signature = FixedAliYunSignatureUtil.calculateSignature(
                APP_SECRET,          // secret
                "POST",              // method
                "application/json",  // accept
                "",                  // contentMD5
                "application/json; charset=utf-8",  // contentType - 与请求头保持一致
                "",                  // date
                signHeaders,         // headers
                API_PATH,            // path
                queryParams,         // queryParams
                null                 // formParams
        );





        // 设置签名到请求头
        signHeaders.put("X-Ca-Signature", signature);
        signHeaders.put("X-Ca-Signature-Headers", signatureHeaders);
        signHeaders.put("Accept", "application/json");
        
        // 构造查询字符串
        String queryString = buildQueryString(queryParams);
        String fullUrl = API_URL + (queryString.isEmpty() ? "" : "?" + queryString);
        System.out.println("完整请求URL: " + fullUrl);

        // 构造请求头
        Headers headers = new Headers.Builder()
                .add("Accept", "application/json")
                .add("Content-Type", "application/json")  // 明确设置Content-Type
                .add("X-Ca-Key", APP_KEY)
                .add("X-Ca-Signature", signature)
                .add("X-Ca-Timestamp", timestamp)
                .add("X-Ca-Nonce", nonce)
                .add("X-Ca-Signature-Headers", signatureHeaders)
                .build();


        // 构造空的请求体
        RequestBody body = RequestBody.create("", MediaType.parse("application/json"));


        // 发送请求
        Request request = new Request.Builder()
                .url(fullUrl)
                .headers(headers)
                .post(body) // 使用POST方法
                .build();
        
        System.out.println("发送POST请求到: " + fullUrl);
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("响应状态码: " + response.code());
            System.out.println("响应消息: " + response.message());
            
            // 打印响应头
            System.out.println("响应头:");
            for (String name : response.headers().names()) {
                System.out.println("  " + name + ": " + response.headers().get(name));
            }
            
            if (!response.isSuccessful()) {
                String errorBody = response.body() != null ? response.body().string() : "无响应体";
                System.out.println("错误响应体: " + errorBody);
                
                // 检查是否有详细的错误信息
                String errorMessage = response.header("X-Ca-Error-Message");
                if (errorMessage != null) {
                    System.out.println("详细错误信息: " + errorMessage);
                }
                
                throw new RuntimeException("API请求失败: " + response.code() + " - " + response.message());
            }
            
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String result = responseBody.string();
                System.out.println("请求成功！响应长度: " + result.length());
                System.out.println("响应内容: " + result);
                return result;
            } else {
                throw new RuntimeException("响应体为空");
            }
        }
    }
    
    /**
     * 构建查询字符串
     * 
     * @param params 参数映射
     * @return 查询字符串
     */
    private String buildQueryString(Map<String, String> params) {
        if (params == null || params.isEmpty()) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!first) {
                sb.append("&");
            }
            
            sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
            if (entry.getValue() != null) {
                sb.append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
            }
            
            first = false;
        }
        
        return sb.toString();
    }
}
